package com.youhome.service.service.impl;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.ResultCode;
import com.youhome.service.mapper.BCompanyMapper;
import com.youhome.service.mapper.BUserMapper;
import com.youhome.service.mapper.BUserRoleMapper;
import com.youhome.service.model.*;
import com.youhome.service.service.BCompanyService;
import com.youhome.service.service.BCounselorService;
import com.youhome.service.service.BUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;

/**
 * Created by chen365 on 2017/10/20.
 */
@Service
public class BCompanyServiceImpl implements BCompanyService {
    @Autowired
    private BUserMapper bUserMapper;
    @Autowired
    private BCompanyMapper bCompanyMapper;
    @Autowired
    private BUserRoleMapper bUserRoleMapper;
    @Autowired
    private BCounselorService bCounselorService;
    @Autowired
    private BUserService bUserService;

    @Override
    @Transactional
    public int register(String phone, BCompany company) {
        int success = 0;
        BUser user = new BUser();

        user.setPhone(phone);
        user.setCtime(new Date());
        user.setEnabled(true);

        //添加用户信息
        if (bUserMapper.insertSelective(user) == 1) {

            Long userid = user.getUserId();

            BUserRole userRole = new BUserRole();
            userRole.setUserId(userid);
            userRole.setRoleId(1L); //ROLE_COMPANY 公司

            //添加用户角色
            if (bUserRoleMapper.insert(userRole) == 1) {
                company.setUuid(UUID.randomUUID().toString());
                company.setUserId(userid);
                company.setCtime(new Date());
                company.setStatus(1);
                //添加公司信息
                success = bCompanyMapper.insertSelective(company);
            }
        }
        return success;
    }

    @Override
    public BCompany selectByUserId(Long userId) {
        return bCompanyMapper.selectByUserId(userId);
    }

    @Override
    public BCompany selectByPrimaryKey(Long companyId) {
        return bCompanyMapper.selectByPrimaryKey(companyId);
    }

    @Override
    public BCompany selectByUUID(String uuid) {
        return bCompanyMapper.selectByUUID(uuid);
    }

    @Override
    public int update(BCompany company) throws Exception {
        company.setUtime(new Date());
        return bCompanyMapper.updateByPrimaryKeySelective(company);
    }

    @Override
    public Long selectCompanyIdByUserId(Long userId) {
        return bCompanyMapper.selectCompanyIdByUserId(userId);
    }

    @Override
    public BCompany selectDetails(Principal principal) throws CustomException {
        return bCompanyMapper.selectByUserId(principal.getUser().getUserId());
    }

    @Override
    public BCompany updateHomePage(Principal principal, BCompany company) throws CustomException {
        BCompany bCompany = bCompanyMapper.selectByUserId(principal.getUser().getUserId());
        if (bCompany == null) {
            throw new CustomException(ResultCode.DATA_DOES_NOT_EXIST_OR_HAS_BEEN_DELETED);
        }
        company.setCompanyId(bCompany.getCompanyId());
        if (bCompanyMapper.updateByPrimaryKeySelective(company) < 1) {
            throw new CustomException(ResultCode.FAIL);
        }
        return bCompany;
    }

    @Override
    public BCompany updateCompany(Principal principal, BCompany company) throws CustomException {
        Long userId = principal.getUser().getUserId();
        company.setUserId(userId);
        company.setUtime(new Date());
        if (bCompanyMapper.updateByPrimaryKeySelective(company) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
        return company;
    }

    @Override
    public String selectUUID(Principal principal) throws CustomException {
        Long userId = principal.getUser().getUserId();
        BCounselor counselor = bCounselorService.selectByUserId(userId);
        if (counselor == null) {
            throw new CustomException(ResultCode.DATA_DOES_NOT_EXIST_OR_HAS_BEEN_DELETED);
        }
        BCompany company = bCompanyMapper.selectByPrimaryKey(counselor.getCompanyId());
        if (company == null) {
            throw new CustomException(ResultCode.DATA_DOES_NOT_EXIST_OR_HAS_BEEN_DELETED);
        }
        return company.getUuid();
    }

    @Override
    public BCompany selectDetailsByUUID(String uuid) throws CustomException {
        if (StringUtils.isEmpty(uuid)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        return bCompanyMapper.selectByUUID(uuid);
    }
}
